<?php 
/**
* 数据库操作类
*/
class Model 
{
	public $table;
	public $where;
	public $orderBy;
	public $limit;
	public static $link;

	function __construct($table='')
	{

		if($table){
			$this->table=$table;
		}
		if(!self::$link){
			self::$link = new PDO(C('DB_TYPE').":host=".C('DB_HOST').";dbname=".C('DB_NAME').";charset=utf8",C('DB_USER'),C('DB_PASSWD'));
		}	
	}
	/*
	**执行查询相关的
	*/
	@	
	public function query($sql)
	{
		$res = self::$link->query($sql);

		if($res){
			return $res->fetchAll(PDO::FETCH_ASSOC);
		}else{
			$this->getError();
		}
	}
	/*
	**执行操作相关的
	*/
	public function exec($sql)
	{
		$res = self::$link->exec($sql);

		if($res){
			return true;
		}else{
			$this->getError();
		}
	}
	//增加数据的方法
	public function add($data)
	{
		// var_dump($data);
		// var_dump($this->table);
		$keyStr = implode(",", array_keys($data));
		$valuesStr = implode("','", $data);

		$sql = "insert into stu (".$keyStr.") values('".$valuesStr."')";

		// 插入成功返回主键
		if($this->exec($sql)){
			return self::$link->lastInsertId();
		};
	}
	//取数据
	public function select($filed="*")
	{

		$sql = "select ".$filed." from ".$this->table.$this->where.$this->orderBy.$this->limit;
		var_dump($sql);
		exit;

		return $this->query($sql);
	}
	// 增加条件
	public function where($where)
	{
		$this->where = " where ".$where;
		return $this;
	}
	//排序
	public function orderBy($orderBy){
		$this->orderBy = " order by ".$orderBy;
		return $this;
	}
	
	public function limit($limit)
	{
		$this->limit = " limit ".$limit;
		return $this;
	}
	public function getError(){
		$info = self::$link->errorInfo();
		exit("错误代码：".$info[1]."错误信息：".$info[2]);
	}
}





















 ?>